
// ToolsLib Project

/* ToolsLib library for RusRoute firewall and other projects of
 * Andrey A. Moiseenko / IE Moiseenko A.A. (Russia)
 * e-mail: support@maasoftware.ru, maa2002@mail.ru
 * web: http://maasoftware.ru, http://maasoftware.com, http://maasoft.ru, http://maasoft.org
 * Author's full name: Andrey Alekseevitch Moiseenko
 * (russian name: Моисеенко Андрей Алексеевич)
 */

// ToolsLib/templates.h

/* Copyright (C) 2002-2024 Andrey A. Moiseenko (support@maasoftware.ru)
 * All rights reserved.
 *
 * This file contains some templates for HRT.
 * The library implementation written
 * by Andrey A. Moiseenko (support@maasoftware.ru).
 * This library and applications are
 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 * as long as the following conditions are aheared to.
 *
 * Copyright remains Andrey A. Moiseenko, and as such any Copyright notices in
 * the code are not to be removed.  If this code is used in a product,
 * Andrey A. Moiseenko should be given attribution as the author of the parts used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Andrey A. Moiseenko (support@maasoftware.ru)
 *
 * THIS SOFTWARE IS PROVIDED BY ANDREY A. MOISEENKO ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */


//#ifndef __unix__
class CHrtMultiTimer : public CMaaTime
{
#define MaxN 64
    _qword m_Time[MaxN];
    _qword m_Mem[MaxN];
    const char * m_Msg[MaxN];
    int m_pos;
public:
    CHrtMultiTimer(CMaaTime * pTime = nullptr)
    :   CMaaTime(pTime ? *pTime : gHRTime)
    {
        m_pos = 0;
    }
    void Start() noexcept
    {
        StartWatch();
    }
    _qword Stop(const char * Msg) noexcept
    {
        const _qword x = GetTime();
        if  (m_pos < MaxN)
        {
            m_Time[m_pos] = x;
            m_Msg[m_pos] = Msg;
            //m_Mem[m_pos] = 0;
            m_Mem[m_pos] = MaaGetMemoryUsage(); //CTOOGetMemoryAllocated();// / 1024;
            m_pos++;
        }
        return x;
    }
    CMaaString GetResult(const char * Header = "")
    {
        CMaaString Ret = Header;
        for (int i = 0; i < m_pos; i++)
        {
            //Ret.Format("%S%10,D us  %9,D Kb  %s\n", &Ret, m_Time[i], m_Mem[i], m_Msg[i]);
            Ret.Format("%S%3,D.%06D us  %14,D  %s\n", &Ret, m_Time[i] / 1000000, m_Time[i] % 1000000, m_Mem[i], m_Msg[i]);
        }
        return Ret;
    }
#undef MaxN
};

//#endif

inline void void_null_func() {}

// CMaaPtInPolly algo is from Habr comment
template<class T = double> int CMaaPtInPollyN(const T& x, const T& y, int N, const T* xx, const T* yy) noexcept
{
    if (N < 3)
    {
        return -1; // N < 3 error
    }
    bool in_polygon = false;
    for (int i = 0; i < N; i++)
    {
        T x0 = xx[i];
        T y0 = yy[i];
        T x1 = xx[i + 1 < N ? i + 1 : 0];
        T y1 = yy[i + 1 < N ? i + 1 : 0];
        if (((y0 <= y && y < y1) || (y1 <= y && y < y0)) && (x > (x1 - x0) * (y - y0) / (y1 - y0) + x0))
        {
            in_polygon = !in_polygon;
        }
    }
    return in_polygon;
}

template<class T = double> int CMaaPtInPolly2N(const T& x, const T& y, int N, const T* xy) noexcept
{
    if (N < 3)
    {
        return -1; // N < 3 error
    }
    bool in_polygon = false;
    for (int i = 0; i < N; i++)
    {
        T x0 = xy[2 * i];
        T y0 = xy[2 * i + 1];
        T x1 = xy[i + 1 < N ? 2 * (i + 1) : 0];
        T y1 = xy[i + 1 < N ? 2 * (i + 1) + 1: 1];
        if (((y0 <= y && y < y1) || (y1 <= y && y < y0)) && (x > (x1 - x0) * (y - y0) / (y1 - y0) + x0))
        {
            in_polygon = !in_polygon;
        }
    }
    return in_polygon;
}

#if 0
void CMaaPtInPolly_Test()
{
    const int N = 5;
    double p[2 * N] = { 0, 0, 100, 0, 100, 100, 50, 0, 0, 100 };
    const int M = 5;
    double m[2 * N] = { 0, 0, 200, 50, 200, 200, 30, 30, 50, 20 };

    double xx[N];
    double yy[N];

    for (int i = 0; i < N; i++)
    {
        xx[i] = p[2 * i];
        yy[i] = p[2 * i + 1];
        __utf8_printf("(%lf, %lf) ", xx[i], yy[i]);
    }
    __utf8_printf("\n");
    const char* msg[3] = { "N < 3 error", "out of polly", "in polly" };
    for (int i = 0; i < M; i++)
    {
        double x = m[2 * i], y = m[2 * i + 1];
        __utf8_printf("(%lf, %lf) - %s - %s\n", x, y, msg[1 + CMaaPtInPollyN(x, y, N, xx, yy)], msg[1 + CMaaPtInPolly2N(x, y, N, p)]);
    }
}
#endif
